Can you use pip for Python2 on Ubuntu 20.04?

Linode Staff

My project is written in Python 2 and I'd like to use pip to manage its dependencies on my Ubuntu 20.04 Linode. I can run apt-get install python2 but I can't find python-pip anywhere in the Ubuntu repositories. Only python3-pip. How do I get pip for Python2?

2 Replies

Ubuntu 20.04 is officially moving forward with Removal of Python 2. As of right now, you can install the basic Python2 libraries, but utilities like pip for Python2 require some creative workarounds if you want to use them.

My recommendation would be to use the virtualenv Python virtual environment tool. It can create an isolated Python development environment, independent of your Ubuntu system Python. The best part is it can create sandboxed Python2 environments even if your distribution doesn't support Python2.

To get started, you'll need to make sure that the basic Python2 libraries are installed on your Linode. You'll also need to install virtualenv as well:

> sudo apt install python2
> sudo apt install virtualenv

You'll then create a directory for your virtual Python 2 environment:

> mkdir /home/yourname/example_env/


Then you intialize a virtual environment in that directory. Make sure to specify the correct Python version here, or it will default to Python 3:

> virtualenv -p /usr/bin/python2.7 /home/yourname/example_env/

If everything went correctly, you should see python2.7 in your environment's lib/ subdirectory:

> ls example_env/lib/
python2.7

Next, you can fire up your virtual Python2 environment:

> source example_env/bin/activate

While in your virtual environment, you should be able to see and use a Python2 version of pip:

(example_env) > which pip
/home/yourname/example_env/bin/pip

(example_env) > pip install numpy

To leave your virtual environment:

(example_env) > deactivate

When not in the virtual environment, you won't be able to see or use pip anymore:

> which pip
(this returns nothing)

This will conveniently isolate all your project dependencies from your system Python (which will mostly default to using Python 3). This not only allows you to use Python 2, but will also prevent ugly dependency conflicts that can happen when you mix Python versions on the same distribution.

You can also globally install pip for python2 on Ubuntu 20.04, as desribed in the second half of the Install Pip on Ubuntu-20.04 guide, though this method may cause package conflicts.

For more information on using virtual environments, check out these other resources:

The next release of Debian ("Bullseye") will remove Python2 as well:

https://lists.debian.org/debian-python/2019/07/msg00080.html

-- sw

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct