1. Overview

When developing projects, we sometimes need a duplicate of an existing virtual environment to test new features or experiment with different configurations. Copying a virtual environment allows us to create a separate and identical environment without impacting the main environment.

Additionally, it helps with backing up and recovering our work and saves time and effort. However, when we replicate a virtual environment, it reinstalls already-installed packages. This means that copying the environment along with reinstalling packages can be time-consuming and bandwidth-intensive, especially when we have limited or slow internet connections.

Moreover, we can use various methods to replicate a virtual environment locally without reinstalling packages via pip. In this tutorial, we’ll explore several methods, such as using virtualenv-clone, cp, pip freeze, and others to duplicate a virtual environment locally.

2. Creating a Sample Virtual Environment

Before we dive into creating a virtual environment, let’s ensure that pipx is installed in our system. We can install it using the apt package manager:

$ sudo apt install pipx

Next, we’ll execute pipx ensurepath to confirm that the pipx installation directory is added to the system’s PATH variable:

$ pipx ensurepath

Now that we have pipx installed, let’s install the virtualenv package using the pipx install command:

$ pipx install virtualenv

Alternatively, we can also use the apt install command to install virtualenv:

$ sudo apt install virtualenv

Furthermore, let’s confirm the virtualenv installation and display the version number of virtualenv installed in our system:

$ virtualenv --version

Next, let’s use the virtualenv command to create a new virtual environment:

$ virtualenv myvirtualenv

We need to replace myvirtualenv in this example with the name we want to give our virtual environment. Additionally, the virtualenv command will create a new folder containing the same name we specified for the virtual environment.

Lastly, let’s activate the virtual environment:

$ source myvirtualenv/bin/activate

Now that we’re inside the virtual environment, we can install packages and work on our projects without affecting the global environment.

Once we finish working in the virtual environment, let’s use the deactivate command to deactivate it:

$ deactivate

After deactivation, we’ll return to our system’s default environment.

3. Using virtualenv-clone

virtualenv-clone is a Python package that allows us to clone a non-relocatable virtual environment. Additionally, we can use it to create a copy of our existing virtual environment.

The virtualenv-clone package creates an exact copy of the old virtual environment, including all installed packages, in the new virtual environment.

Before getting started, we first need to install virtualenv-clone using pipx:

$ pipx install virtualenv-clone

Next, let’s activate the virtual environment we want to clone:

$ source myvirtualenv/bin/activate

Finally, we use the virtualenv-clone command to clone the virtual environment:

$ (newenv): virtualenv-clone myvirtualenv newenv

Here, myvirtualenv environment is the name of the virtual environment we want to clone, and newenv is the name of the cloned virtual environment.

Lastly, we can exit from the virtual environment using the deactivate command:

$ (newenv): deactivate

Now, let’s activate the cloned virtual environment:

$ source newenv/bin/activate

In the opened cloned environment, let’s verify that the clone has the same packages and dependencies as the original virtual environment:

$ pip list

The above pip list command returns a list of the same packages and dependencies as the original virtual environment.

That’s it! We have now successfully created a copy of a virtual environment locally without using pip.

Furthermore, this approach not only saves time and effort, but it also reduces internet bandwidth usage to zero. We can also use this method to replicate a virtual environment when we don’t have access to the network.

4. Using cp

Using the traditional cp command, we can duplicate a virtual environment locally without reinstalling packages. Moreover, we can use this approach to manually copy and edit the virtual environment path.

Let’s first copy the existing virtual environment (myvirtualenv) to a new location:

$ cp -R /home/user/myvirtualenv /home/user/newvirtualenv

Next, we need to open the activate file of the newly copied virtual environment (newvirtualenv) using gedit or any Linux text editor:

$ gedit /home/user/newvirtualenv/bin/activate

Once the file is open, we navigate to the line VIRTUAL_ENV=”/home/user/myvirtualenv” line and update the VIRTUAL_ENV variable with a new virtual environment path:

editing virtual environment path

Lastly, let’s save and close the file. Here, our new virtual environment newvirtualenv has been copied from the myvirtualenv environment and is ready to use. While this method is simple, it requires manual editing and can lead to errors if not done correctly.

5. Using pip freeze

This traditional method is straightforward but involves reinstalling all packages from scratch. However, we can duplicate an existing virtual environment by listing its installed packages with pip freeze and then installing them in the new environment using pip install.

First, we need to generate a requirements.txt file. This file displays all the installed packages and their versions for our current virtual environment. After creating this file, we can recreate the same environment elsewhere.

Let’s start by activating our existing virtual environment:

$ source myvirtualenv/bin/activate

Next, we need to create the requirement.txt file using the pip freeze command:

$ (myvirtualenv): pip freeze > requirements.txt

Furthermore, if we need a different version of a specific package for our new environment, we can modify the version by manually opening and editing our requirements.txt file in a text editor, replacing the existing version number with the new version number. We can also add or remove specific packages from the requirements.txt file.

Next, let’s copy this file to the new location and set up a new environment in the new location. However, if we already have a new virtual environment created, then we need to activate the environment:

$ source newenv/bin/activate

After activating the new environment, we install all the packages specified in the requirements.txt file using the pip install command:

$ (newenv): pip install -r requirements.txt

This method will require that our system is connected to the internet when installing packages.

6. Installing Packages Offline

We can also install any package that’s already installed in our virtual environment to another environment that doesn’t have a network connection. This method is particularly helpful when we want to duplicate the environment and reinstall all packages without accessing the network.

First, we need to activate our existing environment where specified packages are installed:

$ source myvirtualenv/bin/activate

Next, we need to create the requirements.txt file:

$ (myvirtualenv): pip freeze > requirements.txt

Let’s create a new directory with mkdir and copy the requirements.txt file into it:

$ mkdir proj
$ cp requirements.txt proj/

Next, let’s use cd to change our current directory to the recently created directory:

$ cd proj

In the new directory, we need to download all the packages listed in requirements.txt using the pip download command:

$ pip download -r requirements.txt

After downloading packages, we can use this directory and install packages to any other environment that does not have a network connection.

For example, let’s consider a virtual environment (newenv) without an internet connection. Now, let’s enter into the environment by activating it:

$ source newenv/bin/activate

Next, we can install all the packages listed in the requirements.txt file from the specified directory to our new virtual environment:

$ pip install -r proj/requirements.txt --no-index --find-links proj

Furthermore, we can also verify package installation by running the pip list command in the terminal.

7. Conclusion

In this article, we explored various methods to replicate a virtual environment locally without reinstalling packages via pip in Linux. These methods included using virtualenv-clone, cp, and pip freeze commands. We also explored how to install packages offline from a specified directory. All of these methods allow us to get a cloned virtual environment up and running while saving time and bandwidth.

Notify of
Inline Feedbacks
View all comments