How to install virtualenv on Ubuntu 18.04

We may face issues when our Linux distribution only offers certain versions of Python and its packages, when we actually need newer or older versions.

We can install new versions of Python on the server, however this will be more complex because we will have some dependency issues when trying to compile everything we need.

Virtual environments make this very easy to manage and set up, we can have different versions of Python and packages in each environment, and it will be isolated from the main system.

Installing Virtual Environment on Ubuntu 18.04 -or later from 16.04, is fairly easy task and it shouldn’t take more then 10 minutes to finish.

install pip

install pip3 if you don’t have already

<span>$ </span> <span>sudo </span>apt-get <span>install </span>python3-pip
<span>$ </span>  <span>sudo </span>apt-get <span>install </span>python3-pip
$ sudo apt-get install python3-pip

Enter fullscreen mode Exit fullscreen mode

instal virtualenv

<span>$ </span> <span>sudo </span>pip3 <span>install </span>virtualenv
<span>$ </span>  <span>sudo </span>pip3 <span>install </span>virtualenv
$ sudo pip3 install virtualenv

Enter fullscreen mode Exit fullscreen mode

build new virtualenv

<span>$ </span> <span>cd</span> <span>$YOUR_PROJECT_DIRECTORY</span>
<span>$ </span> virtualenv .venv
<span>$ </span>  <span>cd</span> <span>$YOUR_PROJECT_DIRECTORY</span>
<span>$ </span>  virtualenv .venv
$ cd $YOUR_PROJECT_DIRECTORY $ virtualenv .venv

Enter fullscreen mode Exit fullscreen mode

.venv or any $NAME for your virtualenv

to activate your virtualev

<span>$ </span> <span>source</span> .venv/bin/activate
<span>$ </span>  <span>source</span> .venv/bin/activate
$ source .venv/bin/activate

Enter fullscreen mode Exit fullscreen mode

and you see your .venv activated

(.venv) ~/project$
(.venv) ~/project$
(.venv) ~/project$

Enter fullscreen mode Exit fullscreen mode

to deactivate your virtualev

$ deactivate
$   deactivate
$ deactivate

Enter fullscreen mode Exit fullscreen mode

install new packages

After activating your virtualenv

<span>$ </span> pip <span>install</span> <some-package>
<span>$ </span> pip <span>install</span> <some-package>
$ pip install <some-package>

Enter fullscreen mode Exit fullscreen mode

P.S:

You can also use Python2.7 interpreter for your virtualenv but I would not recommend that since Python 2.7 will not be maintained after January 1, 2020. But if you like to:

<span>$ </span> virtualenv <span>-p</span> /usr/bin/python2.7 .venv
<span>$ </span>  virtualenv <span>-p</span> /usr/bin/python2.7 .venv
$ virtualenv -p /usr/bin/python2.7 .venv

Enter fullscreen mode Exit fullscreen mode

原文链接:How to install virtualenv on Ubuntu 18.04

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
Nothing can't be figured out. The past just can't be reached again.
没有什么过不去,只是再也回不去
评论 抢沙发

请登录后发表评论

    暂无评论内容